import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { EdubaseAcademicPeriodApi } from '#/api/edu/base/setting/academicPeriod';
import type { DescriptionItemSchema } from '#/components/description';

import { h } from 'vue';

import { CommonStatusEnum, DICT_TYPE } from '@vben/constants';
import { getDictOptions } from '@vben/hooks';

import { Typography } from 'ant-design-vue';

import { z } from '#/adapter/form';
import { DictTag } from '#/components/dict-tag';
import { getRangePickerDefaultProps } from '#/utils';

const { Paragraph } = Typography;
/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'period',
      label: '学段',
      rules: 'required',
      component: 'Select',
      componentProps(values) {
        return {
          options: getDictOptions(DICT_TYPE.EDU_PERIOD, 'number'),
          placeholder: '请选择学段',
          onChange: (val, option) => {
            if (val) {
              values.periodName = option.label;
              values.sort = option.sort;
            } else {
              values.periodName = '';
            }
          },
        };
      },
    },
    {
      fieldName: 'periodName',
      rules: 'required',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'sort',
      label: '学段顺序',
      rules: 'required',
      component: 'InputNumber',
      componentProps: {
        min: 0,
        placeholder: '请输入学段顺序',
      },
    },
    {
      fieldName: 'gradeInfo',
      label: '年级',
      component: 'CheckboxGroup',
      dependencies: {
        triggerFields: ['period'],
        componentProps(values) {
          return {
            mode: 'multiple',
            key: values.period || '99',
            allowClear: true,
            options: getDictOptions(DICT_TYPE.EDU_GRADE, 'number').filter(
              (item) => {
                if (values.period === null || values.period === undefined) {
                  return true;
                }
                let val = values.period.toString();
                if (val === '5') {
                  val = '4';
                }
                if (val === '6') {
                  val = '7';
                }
                return item.parentValue === val;
              },
            ),
            placeholder: '请选择年级',
          };
        },
      },
    },
    {
      fieldName: 'status',
      label: '是否启用',
      component: 'Select',
      rules: z.number().default(CommonStatusEnum.ENABLE),
      componentProps: {
        options: getDictOptions(DICT_TYPE.COMMON_STATUS, 'number'),
        placeholder: '请选择有效状态',
      },
    },
  ];
}

/** 详情页字段 */
export function useDetailSchema(): DescriptionItemSchema[] {
  return [
    {
      label: '学段编码',
      field: 'period',
    },
    {
      label: '学段名称',
      field: 'periodName',
    },
    {
      label: '学段顺序',
      field: 'sort',
    },
    {
      label: '年级',
      field: 'gradeInfo',
      render: (val) => {
        return h(DictTag, {
          type: DICT_TYPE.EDU_GRADE,
          value: val,
        });
      },
    },
    {
      label: '是否启用',
      field: 'status',
      render: (val) => {
        return h(DictTag, {
          type: DICT_TYPE.COMMON_STATUS,
          value: val,
        });
      },
    },
  ];
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'period',
      label: '学段',
      component: 'Select',
      componentProps: {
        options: getDictOptions(DICT_TYPE.EDU_PERIOD, 'number'),
        placeholder: '请输入学段编码',
        allowClear: true,
      },
    },

    {
      fieldName: 'status',
      label: '是否启用',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.COMMON_STATUS, 'number'),
        placeholder: '请选择是否启用',
      },
    },
    {
      fieldName: 'createTime',
      label: '创建时间',
      component: 'RangePicker',
      componentProps: {
        ...getRangePickerDefaultProps(),
        allowClear: true,
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions<EdubaseAcademicPeriodApi.AcademicPeriod>['columns'] {
  return [
    { type: 'checkbox', width: 40 },
    {
      field: 'id',
      title: '主键ID',
      minWidth: 120,
      visible: false,
    },
    {
      field: 'period',
      title: '学段编码',
      minWidth: 80,
    },
    {
      field: 'periodName',
      title: '学段名称',
      minWidth: 100,
    },
    {
      field: 'sort',
      title: '学段顺序',
      minWidth: 80,
    },
    {
      field: 'gradeInfo',
      title: '年级',
      minWidth: 400,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.EDU_GRADE },
      },
    },

    {
      field: 'status',
      title: '是否启用',
      minWidth: 100,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.COMMON_STATUS },
      },
    },
    {
      field: 'createTime',
      title: '创建时间',
      minWidth: 120,
      formatter: 'formatDateTime',
    },
    {
      field: 'actions',
      title: '操作',
      width: 200,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
